<?php

/*
 * Following code will update a product information
 * A product is identified by product id (pid)
 */

// array for JSON response
$response = array();

// check for required fields
if (isset($_POST['user_id']) && isset($_POST['id_level']) && isset($_POST['rate']) && isset($_POST['score'])) {
    
    $user_id = $_POST['user_id'];
    $id_level = $_POST['id_level'];
    $rate = $_POST['rate'];
    $score = $_POST['score'];

    // include db connect class
    require_once __DIR__ . '/db_connect.php';

    // connecting to db
    $db = new DB_CONNECT();

    // mysql update row with matched pid
    $result = mysql_query("UPDATE game SET rate = '$rate', score = '$score', WHERE user_id = $user_id AND id_level = $id_level");

    // check if row inserted or not
    if ($result) {
        // successfully updated
        $response["success"] = 1;
        $response["responseMessage"] = "Game successfully updated.";
        
        // echoing JSON response
        echo json_encode($response);
    } else {
        
    }
} else {
    // required field is missing
    $response["success"] = 0;
    $response["responseMessage"] = "Required field(s) is missing";

    // echoing JSON response
    echo json_encode($response);
}
?>
